package com.sky.service.impl;

import com.sky.entity.Orders;
import com.sky.mapper.OrderMapper;
import com.sky.mapper.UserMapper;
import com.sky.service.ReportService;
import com.sky.vo.TurnoverReportVO;
import com.sky.vo.UserReportVO;
import lombok.Builder;
import lombok.Data;
import org.apache.commons.lang.StringUtils;
import org.apache.poi.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service

public class ReportServiceImpl implements ReportService {
	
	@Autowired
	private OrderMapper orderMapper;
	
	@Autowired
	private UserMapper userMapper;
	
	/**
	 * 营业额统计
	 * @param begin
	 * @param end
	 * @return
	 */
	public TurnoverReportVO getTurnoverStatistics(LocalDate begin, LocalDate end) {
		
		//该集合用于存放begin到end天数的日期
		List<LocalDate> list = new ArrayList<>();
		list.add(begin);
		while (!begin.equals(end)) {
			//一天一天往后遍历放入list
			begin = begin.plusDays(1);
			list.add(begin);
		}
		
		//以逗号分割de日期字符串
		String join1 = StringUtils.join(list, ",");
		
		List<Double> doubleList = new ArrayList<>();
		for (LocalDate date : list) {
			//查询日期对应营业额数据,已完成的
			LocalDateTime beginTime = LocalDateTime.of(date, LocalTime.MIN);
			LocalDateTime endTime = LocalDateTime.of(date, LocalTime.MAX);
			Map map = new HashMap();
			map.put("beginTime", beginTime);
			map.put("endTime", endTime);
			map.put("status", Orders.COMPLETED);
			
			//select sum(amount) from orders where order_time > beginTime and order < endTime and status = 5
			Double turnover = orderMapper.sumByMap(map);
			turnover = turnover == null ? 0 : turnover;
			doubleList.add(turnover);
		}
		
		//以逗号分割de营业额字符串
		String join2 = StringUtils.join(doubleList, ",");
		
		TurnoverReportVO vo = TurnoverReportVO
				.builder()
				.dateList(join1)
				.turnoverList(join2)
				.build();
		return vo;
	}
	
	/**
	 * 用户统计
	 * @param begin
	 * @param end
	 * @return
	 */
	public UserReportVO getUserStatistics(LocalDate begin, LocalDate end) {
		
		//该集合用于存放begin到end天数的日期
		List<LocalDate> list = new ArrayList<>();
		list.add(begin);
		while (!begin.equals(end)) {
			//一天一天往后遍历放入list
			begin = begin.plusDays(1);
			list.add(begin);
		}
		//以逗号分割de日期字符串
		String join1 = StringUtils.join(list, ",");
		
		//存放每天新增用户数
		List<Integer> newUserList = new ArrayList<>();
		//统计每天总用户数
		List<Integer> totalUserList = new ArrayList<>();
		
		
		for (LocalDate date : list) {
			//查询日期对应营业额数据,已完成的
			LocalDateTime beginTime = LocalDateTime.of(date, LocalTime.MIN);
			LocalDateTime endTime = LocalDateTime.of(date, LocalTime.MAX);
			
			Map map = new HashMap();
			map.put("endTime", endTime);
			//总用户数
			Integer totalUser = userMapper.countByMap(map);
			
			map.put("beginTime", beginTime);
			//新增用户数
			Integer newUser = userMapper.countByMap(map);
			
			newUserList.add(newUser);
			totalUserList.add(totalUser);
		}
		
		String join2 = StringUtils.join(newUserList, ",");
		String join3 = StringUtils.join(totalUserList, ",");
		
		UserReportVO vo = UserReportVO
				.builder()
				.dateList(join1)
				.newUserList(join2)
				.totalUserList(join3)
				.build();
				
			return vo;
	}
}
